<template>
    <div class="app-container">
      <div class="filter-container">
        <el-input v-model="listQuery.keyword" clearable placeholder="渠道名" style="width: 200px;"
                  class="filter-item" @keyup.enter.native="handleFilter" />
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search"
                   @click="handleFilter">
          搜 索
        </el-button>
        <el-button class="filter-item" style="margin-left: 10px;" type="primary"
                   icon="el-icon-edit" @click="handleCreate">
          新 增
        </el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="id" prop="id" align="center" width="80">
          <template slot-scope="{row}">
            <span>{{ row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="渠道名称" prop="name" align="center">
          <template slot-scope="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="name" align="center">
          <template slot-scope="{row}">
            <span>{{ row.createdAt }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
          <template slot-scope="{row,$index}">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              编 辑
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row)">
              删 除
            </el-button>
            <el-button size="mini" type="warning" @click="handleReset(row)">
              重置密码
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.limit" @pagination="getList" />

      <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form ref="dataForm" :rules="rules" :model="canal" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">

          <el-form-item label="渠道名" prop="name">
            <el-input v-model="canal.name" />
          </el-form-item>

          <el-form-item label="账户" prop="buzName">
            <el-input v-model="canal.buzName" />
          </el-form-item>

          <el-form-item v-if="dialogStatus==='新增'" label="密码" prop="buzPassword">
            <el-input v-model="canal.buzPassword" />
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">
            取 消
          </el-button>
          <el-button type="primary" @click="dialogStatus==='新增'?createData():updateData()">
            确认
          </el-button>
        </div>
      </el-dialog>

      <el-dialog title="删除" :visible.sync="dialogDelVisible">
       <p>是否删除该渠道号？</p>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogDelVisible = false">
            取 消
          </el-button>
          <el-button type="primary" @click="deleteChannel()">
            确认
          </el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
import { fetchList, createCanal, detail, updateCanal, deleteCanal, resetPwd } from '@/api/canal/channel'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'

export default {
  name: 'Canal',
  components: { Pagination },
  directives: { waves },

  data() {
    return {
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        keyword: ''
      },
      canal: {
        id: null,
        name: null,
        buzName: null,
        buzPassword: null
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        name: [{ required: true, message: '渠道商名称不能为空', trigger: 'blur'}],
        buzName: [{ required: true, message: '渠道商账户不能为空', trigger: 'blur' }],
        buzPassword: [{ required: true, message: '渠道商账户不能为空', trigger: 'blur' }]
      },
      dialogDelVisible: false,
      wellDel: undefined
    }
  },
  created() {
    const that = this
    that.getList()
  },
  methods: {
    getList() {
      const that = this
      that.listLoading = true
      const { keyword, pageNo, pageSize } = that.listQuery
      fetchList({ keyword: keyword, pageNo: pageNo, pageSize: pageSize }).then(response => {
        that.list = response.data.dataList
        that.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      const that = this
      that.listQuery.pageNo = 1
      that.getList()
    },
    resetCanal() {
      this.canal = {
        id: undefined,
        name: null,
        buzName: null,
        buzPassword: null
      }
    },
    handleCreate() {
      this.resetCanal()
      this.dialogStatus = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      detail(row.id).then(response => {
        if (response.code === 200) {
          this.canal = response.data
        }
      })
      this.dialogStatus = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createCanal(this.canal).then((response) => {
            if (response.code !== 200) {
              this.$notify({
                title: '失败',
                message: response.message,
                type: 'fail',
                duration: 2000
              })
            } else {
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            }
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateCanal(this.canal).then((response) => {
            if (response.code !== 200) {
              this.$notify({
                title: '失败',
                message: response.message,
                type: 'fail',
                duration: 2000
              })
            } else {
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.dialogDelVisible = true
      this.wellDel = row.id
    },
    deleteChannel() {
      deleteCanal(this.wellDel).then(response => {
        if (response.code === 200) {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.dialogDelVisible = false
          this.getList()
        } else {
          this.$notify({
            title: '失败',
            message: response.message,
            type: 'fail',
            duration: 2000
          })
          this.dialogDelVisible = false
        }
      })
    },
    handleReset(row) {
      resetPwd(row.id).then(response => {
        if (response.code === 200) {
          this.$notify({
            title: '成功',
            message: '密码重置成功',
            type: 'success',
            duration: 2000
          })
        } else {
          this.$notify({
            title: '失败',
            message: response.message,
            type: 'fail',
            duration: 2000
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
